<template>
  <div>
    <!-- 预授权经销商维护 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="2" :collapse="collapse" labelWidth="90" @filterChange="changeHandle"> </TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :fetchapi="fetchapi" :datakey="'records'" :onColumnsChange="columns => (this.columns = columns)"  :is-select-column="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
        <multiuse-button type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
        <multiuse-button size="small" type="primary"  :loading="exportLoading" :click="downloadTemplate.bind()">下载模板</multiuse-button>
        <multiuse-button size="small" icon="el-icon-upload" @click="showImportFun()">导入</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visibleAdd" :title="'预授权经销商新增'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
    </BaseDialog>
    <BaseDialog :visible.sync="showImport" :closable="false" icon="el-icon-upload" title="导入" width="800px" destroyOnClose :containerStyle="{ height: 'calc(100% - 120px)', overflow: 'auto', paddingBottom: '60px' }">
      <importExcel @close="showImport = false" @importclose="editsave()" url="/api/cms_claimfac/rPPreAuthArea/importExcel"/>
    </BaseDialog>
    <BaseDialog :visible.sync="visibleEdit" :title="'预授权经销商编辑'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <EditForm @close="closeEdit" @save="saveEdit" :rowData="rowData"></EditForm>
    </BaseDialog>
    <BaseDialog :visible.sync="dealerFlag" :closable="false" :title="'经销商选择'" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <DealerList @close="closeDealer" @save="saveDealer"></DealerList>
    </BaseDialog>
  </div>
</template>
<script>
//引入字典
import { dictionary } from '@/utils/dictMixin';
import AddForm from './AddForm.vue';
import EditForm from './EditForm.vue';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { exportExcelFile } from '@/utils/index';
import importExcel from '@/pages/claimDemage/tools/importExcel.vue';
import { getListByPage, getPartType, addOrders, excelExport, getListForExport, deleteOne, downloadModel,exporturl } from '@/api/claim/factory/AuthDealerMaintain';
import { getPart } from '@/api/dictionary/index';
export default {
  mixins: [dictionary],
  components: {
    AddForm,
    DealerList,
    importExcel,
    EditForm
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      visibleEdit: false,
      dealerFlag: false,
      rowData: {},
      showImport: false,
      exportLoading: false,
      infoId: null,
      fetchapi: getListByPage,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      res: []
    };
  },
  created() {
    // this.chuqi();
  },
  methods: {
    closeDealer(){
    this.dealerFlag=false;
    },
    saveDealer(obj) {
  this.topFilterInit.map(x => {
    if ('dealerCode' == x.fieldName) {
      x.initialValue = obj[0].dealerCode;
    }
    if ('dealerName' == x.fieldName) {
      x.initialValue = obj[0].dealerName;
    }

  })
  this.dealerFlag=false;
},
    chooseDealerNo() {
      this.dealerFlag = true;
    },
    async chuqi(){
      const res = await getPart();
      if (res.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'typeId').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'typeId').itemList = res.data.map(y => ({ text: y.name, value: y.code }));
      }
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    closeEdit() {
      this.visibleEdit = false;
    },
    saveEdit() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    add() {
      this.visibleAdd = true;
    },
    editsave() {
      this.showImport = false;
      this.fetchParams = { ...this.fetchParams };
    },
    edit(data) {
      this.rowData = data;
      this.visibleEdit = true;
    },
    // 删除
    async delFun(data) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
         //this.res = await deleteOne({ dealerCode: data.dealerCode });
          this.res  = await deleteOne(data.dealerCode);
          if (this.res.resultCode != 200) {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
          } else {
            this.$notify.success({ title: '提示信息', message: '删除成功!' });
            this.fetchParams = { ...this.fetchParams };
          }
        })
    },
    // Excel更新导入
    showImportFun() {
      this.showImport = true;
    },
    //下载模板
    downloadTemplate() {
      this.$confirm('此操作将下载excel模板, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.exportLoading = true;
        downloadModel().then(res => {
          exportExcelFile(res.data, '预授权经销商导入模板');
          this.exportLoading = false;
        });
      });
    },
    // 导出
   async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000113' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请输入经销商代码...',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
              <multiuse-button icon="el-icon-search" onClick={() => this.chooseDealerNo()} />
            </div>
          );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入经销商名称...',
          initialValue: ''
        },
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: (props, h) => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.delFun(props.row)}>
                  删除
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '经销商代码', dataIndex: 'dealerCode', sorter: true },
        { title: '经销商名称', dataIndex: 'dealerName', sorter: true },
        { title: '创建人', dataIndex: 'createdBy', sorter: true },
        { title: '创建时间', dataIndex: 'createdAt', sorter: true },
        { title: '修改人', dataIndex: 'updatedBy', sorter: true },
        { title: '修改时间', dataIndex: 'updatedAt', sorter: true },
        // { title: '主键', dataIndex: 'aaId', sorter: true, hidden: true }
      ];
    }
  }

};
</script>

<style lang="less" scoped></style>
